package com.jsptpd.smartauth.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsptpd.smartauth.modules.business.model.Collector;
import com.jsptpd.smartauth.modules.business.utils.Constants;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpUtils {

    public static Map getMap(String url) throws IOException {
        Element body = Jsoup
                .connect(url)
                .header("Accept-Language", "zh-CN,zh;q=0.8")
                .timeout(2000)
                .get().body();
        //将body转换为json
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.readValue(body.text(), Map.class);
        return map;
    }

    public static String getString(String url) throws IOException {
        return Jsoup
                .connect(url)
                .header("Accept","text/*")
                .timeout(3000)
                .get().body().text();
    }

    public static String getTokenInHeaders(HttpServletRequest httpServletRequest){
        String tokenId = httpServletRequest.getHeader(Constants.AUTH_HEADTAG_TOKEN);
        if(tokenId!=null){
            try {
                return tokenId.substring(Constants.AUTH_HEADTAG_TOKEN_BEAR.length());
            } catch (Exception e){

            }
        }
        return null;
    }

    public static void setRespToken(String appId,String userId,HttpServletResponse httpServletResponse) throws Exception {
        //发布token并设置响应头
        String jwtToken = JWTUtil.createToken(appId,userId);
        httpServletResponse.addHeader(Constants.AUTH_HEADTAG_TOKEN,Constants.AUTH_HEADTAG_TOKEN_BEAR+jwtToken);
    }

    public static String getUrlEncode(String url) throws UnsupportedEncodingException {
        if(url == null) {
            return null;
        }

        final String reserved_char = ";/?:@=&";
        String ret = "";
        for(int i=0; i < url.length(); i++) {
            String cs = String.valueOf( url.charAt(i) );
            if(reserved_char.contains(cs)){
                ret += cs;
            }else{
                ret += URLEncoder.encode(cs, "utf-8");
            }
        }
        return ret.replace("+", "%20");
    }

    //信息采集的请求头数据
    public static Collector getCollectorHeader(HttpServletRequest httpServletRequest,String userId,String[] defaultHeaders){
        Collector collector = new Collector();

        String[] preHeaders = Constants.COLLECT_SIM_DEFAULT;
        if(defaultHeaders!=null && defaultHeaders.length>=7){
            preHeaders = defaultHeaders;
        }

        collector.setDeviceId(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_DEVICE), preHeaders[0]));
        collector.setAppId(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_APPID), preHeaders[1]));
        collector.setAppVersion(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_VERSION), preHeaders[2]));
        collector.setAppSystem(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_SYSTEM), preHeaders[3]));
        collector.setAppBrand(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_BRAND), preHeaders[4]));
        collector.setAppModel(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_MODEL), preHeaders[5]));
        collector.setToken(StringUtil.getValue(httpServletRequest.getHeader(Constants.AUTH_HEADTAG_TOKEN), preHeaders[6]));
        collector.setUserId(userId);
        collector.setUpdateTime(new Date());

        return collector;
    }
}
